package com.cjm.one.webmanager.controller.order;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.result.Result;
import com.cjm.one.model.base.PageVo;
import com.cjm.one.model.order.dto.OrderChartItem;
import com.cjm.one.model.order.dto.OrderNoRefundDto;
import com.cjm.one.model.order.entity.OrderInfo;
import com.cjm.one.model.order.enums.OrderStatusEnum;
import com.cjm.one.model.order.enums.PayTypeEnum;
import com.cjm.one.model.order.vo.OrderInfoVo;
import com.cjm.one.webmanager.service.order.OrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Tag(name = "3-订单信息")
@RequestMapping("/manager/order/info")
public class OrderInfoController {
    @Resource
    private OrderInfoService orderInfoService;

    @GetMapping("/list")
    @Operation(summary = "订单列表")
    public Result<PageVo<OrderInfoVo>> list(
            @RequestParam(value = "current", required = true, defaultValue = "1") Long current,
            @RequestParam(value = "size", required = true, defaultValue = "10") Long size,
            @RequestParam(value = "userId",required = false) Long userId,
            @RequestParam(value = "orderNo",required = false) String orderNo,
            @RequestParam(value ="status",required = false) OrderStatusEnum status,
            @RequestParam(value ="payStatus",required = false) PayTypeEnum payStatus,
            @RequestParam(value ="userName",required = false) String userName,
            @RequestParam(value ="createTimeStart",required = false) String createTimeStart,
            @RequestParam(value ="createTimeEnd",required = false) String createTimeEnd,
            @RequestParam(value ="phone",required = false) String phone,
            @RequestParam(value ="goodsName",required = false) String goodsName
    ) {
        return orderInfoService.list(
                current,
                size,
                userId,
                orderNo,
                status,
                payStatus,
                userName,
                createTimeStart,
                createTimeEnd,
                phone,
                goodsName);

    }

    @GetMapping("/{id}")
    @Operation(summary = "订单详情")
    public Result<OrderInfoVo> getOrderInfo(@PathVariable("id") Long id) {
        return orderInfoService.getOrderInfo(id);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除订单")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return orderInfoService.delete(id);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除订单")
    public Result<Boolean> deleteBatch(@RequestParam("ids") Long[] ids) {
        return orderInfoService.deleteBatch(ids);
    }
    @PostMapping("/refund/{id}")
    @Operation(summary = "退款")
    public Result<Boolean> refund(@PathVariable("id") Long id) {
        return orderInfoService.refund(id);
    }
    @PutMapping("/status/{id}")
    @Operation(summary = "修改订单状态")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @RequestParam("status") OrderStatusEnum status) {
        return orderInfoService.updateStatus(id, status);
    }

    @PutMapping("/noRefund")
    @Operation(summary = "拒绝退款接口")
    public Result<Boolean> noRefund(@RequestBody OrderNoRefundDto orderNoRefundDto){
        return orderInfoService.noRefund(orderNoRefundDto);
    }

    @GetMapping("/summary")
    @Operation(summary = "订单统计")
    public Result<Map<String, Object>> getOrderSummary(@RequestParam(defaultValue = "30d") String timeRange){
            return orderInfoService.getOrderSummary(timeRange);
    }

    @GetMapping("/chart")
    @Operation(summary = "获取图表数据")
    public Result<List<OrderChartItem>> getOrderChart(
            @RequestParam(defaultValue = "30d") String timeRange){
        return orderInfoService.getOrderChart(timeRange);
    }

    @GetMapping("/test")
    public Result<OrderStatusEnum> test() {
        return orderInfoService.test();
    }
}
